package one.bugu.android.demon.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;

import one.bugu.android.demon.R;

public class SoundPlayUtils {
    // SoundPool对象
    private static SoundPool mSoundPlayer;
    @SuppressLint("StaticFieldLeak")
    private static SoundPlayUtils soundPlayUtils;

    /**
     * 初始化
     *
     * @param context
     */
    public static void init(Context context) {
        if (soundPlayUtils == null) {
            soundPlayUtils = new SoundPlayUtils();
        }
        if (mSoundPlayer == null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mSoundPlayer = new SoundPool.Builder().setMaxStreams(10).build();
            } else {
                mSoundPlayer = new SoundPool(10,
                        AudioManager.STREAM_SYSTEM, 1);
            }
        }
        // 初始化声音
        mSoundPlayer.load(context, R.raw.alipay, 1);
    }

    /**
     * 播放声音
     */
    public static void play() {
        if (mSoundPlayer != null) {
            mSoundPlayer.play(1, 1, 1, 0, 0, 1);
        }
    }
}
